<template>
  <div class="param-main">
    <el-form ref="tblMain" :model="tblMain" labelPosition="right" inline="" label-suffix=":" label-width="100px">
      <el-row :span="24">
        <el-col :span="6">
          <el-form-item label="角色名称">
            <el-input v-model="tblMain.param.useCarDepartment" size="small" placeholder=""></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <div class="page-pane2">
        <el-button type="primary" size="small" @click="initTableData()">查询</el-button>
        <el-button size="small" @click="dataReset()">清空</el-button>
        <el-button size="small" type="info" @click="handleAdd()">新增</el-button>
        <el-button size="small" type="danger" @click="deleteEvent()">删除</el-button>
      </div>
    </el-form>
    <div>
      <el-table ref="tblMain" style="margin-top: 10px;" :data="tblMain.items" v-loading.body="tblMain.loading" border
        stripe>
        <el-table-column type="index" width="80" label="序号" align="center"></el-table-column>
        <el-table-column prop="number" label="登录名称" align="center"></el-table-column>
        <el-table-column prop="parentNumber" label="用户姓名" align="center"></el-table-column>
        <el-table-column prop="parentName" label="角色" align="center"></el-table-column>
        <el-table-column prop="parentName" label="机构" align="center"></el-table-column>
        <el-table-column prop="icon" width="100" label="排序" align="center"></el-table-column>
        <el-table-column prop="icon" width="100" label="状态" align="center"></el-table-column>
      </el-table>
      <el-pagination style="margin-top: 10px" v-show="tblMain.total != 0" :current-page.sync="tblMain.currentPage"
        :page-size="tblMain.pageSize" :total="tblMain.total" layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
    </div>
    <role-dialog ref="modalForm" @ok="modalFormOk"></role-dialog>
    <role-function-dialog ref="roleFunction" @ok="modalFormOk"></role-function-dialog>
  </div>
</template>
<script>
import RoleDialog from './components/RoleDialog'
import RoleFunctionDialog from './components/RoleFunctionDialog'
import tool from '@/tool'
import {
  sysOrganizationDel,
  sysOrganizationList
} from '@/api/index'
export default ({
  name: 'LogList',
  components: {
    RoleDialog,
    RoleFunctionDialog
  },
  data() {
    return {
      tblMain: tool.genTableData({
        action: {
          load: 'queryTblMain'
        },
        param: {
          useCarType: '',
          state: '',
          useCarDepartment: '',
          dispatchState: '',
          carUser: '',
          planTime: null
        },
        items: [{ useCarDepartment: "22222" }, { useCarDepartment: "22222" }, { useCarDepartment: "22222" }, { useCarDepartment: "22222" }, { useCarDepartment: "22222" }, { useCarDepartment: "22222" },
        { useCarDepartment: "22222" }, { useCarDepartment: "22222" }, { useCarDepartment: "22222" }, { useCarDepartment: "22222" }]
      }),
    }
  },
  mounted() {
    // this.init()
    // this.queryCarType()
    // this.queryOrderStatus()
    // this.querySendCarStatus()
    this.tblMain.total = 100
  },
  methods: {
    init() {
      this.initTableData()
    },
    handleAdd() {
      this.$refs.modalForm.add();
      this.$refs.modalForm.title = "新增";
    },
    handleEdit() {
      this.$refs.modalForm.add();
      this.$refs.modalForm.title = "编辑";
    },
    handleSetFunction() {
      this.$refs.roleFunction.add();
    },
    async deleteEvent() {
      if (this.tblMain.selection.length == 0) {
        this.$message.warning('请选择数据')
        return
      }
      this.$confirm('此操作将删除选中数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await this.deleteTblMain()
      }).catch(() => {
        // this.$message({
        //   type: 'info',
        //   message: '已取消删除'
        // })
      })
    },
    // 删除数据
    deleteTblMain() {
      let me = this
      let params = []
      this.tblMain.selection.forEach((element, index) => {
        params.push({
          id: element.id
        })
      })
      me.tblMain.loading = true
      sysOrganizationDel(params).then(res => {
        me.tblMain.loading = false
        let data = res.data
        if (data && data.code == 1) {
          me.$message.success('删除成功')
          this.initTableData()
        }
      }).catch(() => {
        me.$message.error('服务器未响应,请稍后重试')
        me.tblMain.loading = false
      })
    },
    dataReset() {
      this.tblMain.currentPage = 1
      this.tblMain.pageSize = 10
      this.tblMain.param.useCarType = ''
      this.tblMain.param.state = ''
      this.tblMain.param.useCarDepartment = ''
      this.tblMain.param.dispatchState = ''
      this.tblMain.param.carUser = ''
      this.tblMain.param.planTime = null
      this.initTableData()
    },
    handleSelectionChange(val) {
      this.tblMain.selection = val;
    },
    handleSizeChange(val) {
      this.tblMain.pageSize = val
      this.initTableData()
    },
    handleCurrentChange(val) {
      this.tblMain.currentPage = val
      this.initTableData()
    },
    initTableData() {
      let me = this
      me.tblMain.selection = []
      let state = []
      if (me.tblMain.param.state) {
        state[0] = me.tblMain.param.state
      }
      let param = {
        useCarType: me.tblMain.param.useCarType,
        state: state,
        useCarDepartment: me.tblMain.param.useCarDepartment,
        dispatchState: me.tblMain.param.dispatchState,
        carUser: me.tblMain.param.carUser,
        planOutTime: me.tblMain.param.planTime != null ? me.tblMain.param.planTime[0] : '',
        planReturnTime: me.tblMain.param.planTime != null ? me.tblMain.param.planTime[1] : '',
        pageNum: me.tblMain.currentPage,
        pageSize: me.tblMain.pageSize
      }
      me.tblMain.loading = true
      sysOrganizationList(param).then(res => {
        me.tblMain.loading = false
        let data = res.data
        if (data && data.code == 1) {
          me.tblMain.items = data.data.list
          me.tblMain.total = data.data.total
        }
      }).catch(() => {
        me.$message.error('服务器繁忙')
        me.tblMain.loading = false
      })
    },
    detailTblMain(editMode, detailMode) {
      let item = {}
      if (editMode && detailMode) {
        if (this.tblMain.selection.length != 1) {
          this.$message.warning('请选择一条数据')
          return
        }
        item = this.tblMain.selection[0]
      } else if (editMode && !detailMode) {
        if (this.tblMain.selection.length != 1) {
          this.$message.warning('请选择一条数据')
          return
        }
        item = this.tblMain.selection[0]
      }
      this.$emit('page-event', {
        type: 'addPage',
        data: {
          editMode,
          detailMode,
          item
        }
      })
    }
  }
})
</script>
